Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

192
Visualizações
Typecsript and NextJS how to check if "document" exists

NextJS renders part of the code server-side. I can handle that. But I would need to check if cookies are set and here I run into problems. I tried:

!!document && !!document.cookie

and

document !== undefined && !!document.cookie

Each time I get the same error:

ReferenceError: document is not defined

I just want to make this clear, I do not want to render the component exclusively client side. The only thing I want to check is if document.cookie is defined or not :)

I am thankful for every answer that would help me solve this problem


UPDATE:

I accepted the first answer since it was the correct one. However, I realised my approach was wrong and solved it by explicitly checking the type of document:

typeof document !== 'undefined' && !!document.cookie

I guess I did not have enough coffee yet and I missed something pretty obvious.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Next.js uses pre-rendering, so you should wrap any use of document in the useEffect hook as document is not available on the server. That way the particular code gets executed just client-side.

https://nextjs.org/docs/migrating/from-create-react-app#safely-accessing-web-apis

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda